package com.person.ntv.controller.sys;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.github.pagehelper.Page;
import com.person.common.PageParams;
import com.person.easyui.EasyUItools;
import com.person.easyui.TreeNodeRule;
import com.person.exception.ServiceException;
import com.person.ntv.entity.sys.SysDept;
import com.person.ntv.entity.sys.SysOrg;
import com.person.ntv.service.sys.CommonService;
import com.person.ntv.service.sys.SysDeptService;
import com.person.ntv.service.sys.SysOrgService;

@Controller
@RequestMapping("/system/sysDept")
public class SysDeptController {
	@Autowired
  private CommonService commonService;
	@Autowired
  private SysDeptService sysDeptService;
	@Autowired
  private SysOrgService sysOrgService;
  
  @RequestMapping("/listPage")
	public String  getDeptListPage(Map<String,String> params,ModelMap mv){
	    initCode(mv);
		return "/system/dept/deptList";
	}
	
	@RequestMapping("/deptList")
	@ResponseBody
	public Map<String,Object> getDeptList(PageParams myPage){
		Page<Map<String,Object>> dataList=sysDeptService.getDeptPage(myPage);
		for(int i=0;i<dataList.size();i++){
			Object pId=dataList.get(i).get("_parentId");
			boolean flag=true;
			for(int k=0;k<dataList.size();k++){
				 if(String.valueOf(dataList.get(k).get("id")).equals(String.valueOf(pId)))
					  flag=false;
			}
			if(flag)
				dataList.get(i).put("_parentId", null);
		}
      Map<String,Object> map=new HashMap<String,Object>();
      map.put("total",dataList.getTotal());
      map.put("rows",dataList.getResult());
	  return map;
	}
	
	@RequestMapping("/addPage")
	public String  addDeptPage(String pId,ModelMap mv){
		if(pId!=null){
		SysDept dept=sysDeptService.selectByPrimaryKey(new BigDecimal(pId));
		mv.addAttribute("pId", pId);
		mv.addAttribute("orgId",dept.getOrgId());
		}
		initCode(mv);
		return "/system/dept/deptAdd";
	}
	
	@RequestMapping("/editPage")
	public String  editDeptPage(String id,ModelMap mv){
		try {
	    initCode(mv);
	    SysDept dept = sysDeptService.selectByPrimaryKey(new BigDecimal(id));
	    mv.addAttribute("dept",dept);
		} catch (ServiceException e) {
			mv.addAttribute("error", "1");
			mv.addAttribute("msg",e.getMessage()+"查询出错！");
		}
		return "/system/dept/deptEdit";
	}
	
	@RequestMapping("/addDept")
	public String  addDept(ModelMap mv,SysDept sysDept){
		try {
			if(sysDept.getUpDeptId()==null){
				sysDept.setUpDeptId("0");
				sysDept.setUpDeptIds("0/");
			}else{
				sysDept.setUpDeptIds(sysDeptService.selectByPrimaryKey(sysDept.getUpDeptId()).getUpDeptIds()+sysDept.getUpDeptId()+"/");
			}
			sysDeptService.insertSelective(sysDept); 
			mv.addAttribute("error", "0");
			mv.addAttribute("msg","保存成功！");
		} catch (ServiceException e) {
			mv.addAttribute("error", "1");
			mv.addAttribute("msg",e.getMessage()+"保存失败！");
		}
		return "/system/dept/deptAdd";
	}
	
	@RequestMapping("/editDept")
	public String  editDept(ModelMap mv,SysDept sysDept){
		try {
			sysDeptService.updateSysDept(sysDept); 
			mv.addAttribute("error", "0");
			mv.addAttribute("msg","保存成功！");
		} catch (ServiceException e) {
			mv.addAttribute("error", "1");
			mv.addAttribute("msg",e.getMessage()+"保存失败！");
		}
		return "/system/dept/deptEdit";
	}
	
	@RequestMapping("/delDept")
	@ResponseBody
	public Map<String,Object>  delDept(BigDecimal[]ids,HttpServletRequest request){
		Map<String,Object> map=new HashMap<String,Object>();
		try {
			sysDeptService.deleteByPrimaryKeys(ids); 
			map.put("error", "0");
			map.put("msg","删除成功！");
		} catch (ServiceException e) {
			map.put("error", "1");
			map.put("msg",e.getMessage()+"删除失败！");
		}
		return map;
	}
	
	@RequestMapping("/detailPage")
	public String  detailDeptPage(String id,ModelMap mv){
		try {
	    SysDept dept = sysDeptService.selectByPrimaryKey(new BigDecimal(id));
	    SysOrg org=sysOrgService.selectByPrimaryKey(dept.getOrgId());
	    mv.addAttribute("dept",dept);
	    mv.addAttribute("org", org);
		} catch (ServiceException e) {
			mv.addAttribute("error", "1");
			mv.addAttribute("msg",e.getMessage()+"查询出错！");
		}
		return "/system/dept/deptDetail";
	}
  
	
	@RequestMapping("/validateDeptName")
	@ResponseBody
	public boolean validateDeptName(SysDept  sysDept) throws Exception{
		try {
			if(sysDept.getDeptName()==null)sysDept.setDeptName("");
			int cnt=commonService.validateOne("sys_dept","dept_name", sysDept.getDeptName(),sysDept.getId()==null?null:String.valueOf(sysDept.getId()));
			if(cnt==0)
			return true;
		} catch (ServiceException e) {
           e.printStackTrace();
		}
		return false;
	}
	
	@RequestMapping("/validateDeptAbbr")
	@ResponseBody
	public boolean validateDeptAbbr(SysDept  sysDept) throws Exception{
		try {
			if(sysDept.getDeptAbbr()==null)sysDept.setDeptAbbr("");
			int cnt=commonService.validateOne("sys_dept","dept_abbr",sysDept.getDeptAbbr(),sysDept.getId()==null?null:String.valueOf(sysDept.getId())); 
			if(cnt==0)
			return true;
		} catch (ServiceException e) {
			e.printStackTrace();
		}
		return false;
	}
	
	@RequestMapping("/validateDeptCode")
	@ResponseBody
	public boolean validateDeptCode(SysDept  sysDept) throws Exception{
		try {
			if(sysDept.getDeptCode()==null)sysDept.setDeptCode("");
			int cnt=commonService.validateOne("sys_dept","dept_code",sysDept.getDeptCode(),sysDept.getId()==null?null:String.valueOf(sysDept.getId())); 
			if(cnt==0)
			return true;
		} catch (ServiceException e) {
			e.printStackTrace();
		}
		return false;
	}
	
	private void initCode(ModelMap mv){
		List<SysOrg> orgList= sysOrgService.selectAll();
		SysOrg org=new SysOrg();
		org.setOrgAbbr("请选择");
		orgList.add(0,org);
		mv.addAttribute("orgList",JSONArray.fromObject(EasyUItools.getUItreeList(EasyUItools.getTreeNodeList(orgList,new TreeNodeRule("id", "upOrgId","orgAbbr",null,null,null)))).toString());
		mv.addAttribute("SysDeptStatus",JSONArray.fromObject(commonService.getCodeList("SysDeptStatus",true)).toString());
	}
}
